<?php
namespace Api\Controller;
use Think\Controller;
class ItemController extends CommonController {
	public function _initialize(){
		
	}
	//页面显示接口
    public function check(){
		$id = I('post.id');

		$Intro = M('Ware_intro');
		$res = $Intro->where("ware_id={$id}")->find();
		if( $res == false){
			$this->response( false , 1 , '没有数据' , true);
		}
		else{
			$data = array(
				'content'	=> $res['content']
			);
			$this->response( true , 0 , $data , true);
		}
       
    }
    //添加到购物车所加内容
    public function add(){
		$id			= I('post.id');
		$cart_num	= I('post.cart_num');
		$ware_cart1	= I('post.ware_cart1');
		$cart_list1	= I('post.cart_list1');
		
		if( $cart_list1 == 'null'){
			$this->response( false , 2 , '没有规格' , true);
		}
		$Ware		= M('Ware');
		$Shopcar	= M('Ware_shopcar');
		$res = $Shopcar->create();
		$data = array();//创建空数组
		if( $res == false){
			$this->response( false , 1 , '没有数据' , true);
		}
		else{
			$ret= $Shopcar->where("ware_id={$id} and cart_list='{$cart_list1}' and order_id=0")->find();
			if( $ret == false){
				$list = $Ware->where("id={$id}")->find();
				
				$data = array(
					'ware_id'		=>	$id,
					'cart_num'		=>	$cart_num,
					'ware_cart'		=>	$ware_cart1,
					'cart_list'		=>	$cart_list1,
					'head_img'		=>	$list['head_img'],
					'price'			=>	$list['price'],
					'ware_name'		=>	$list['ware_name'],
					'totalprice'	=>	$list['price']*$cart_num
				);
				$res = $Shopcar->add($data);
			}
			else{
				$cart_num = $ret['cart_num']+$cart_num;
				$data = array(
					'cart_num'		=>	$cart_num,
					'totalprice'	=>	$list['price']*$cart_num
				);
				$res = $Shopcar->where("ware_id={$id} and cart_list='{$cart_list1}'")->save($data);
			}
			$this->response( true , 0 , '商品已成功加入购物车' , true);
		}
		
	}
	
	//立即购买添加内容
	public function buycheck(){
		$id			= I('post.id');
		$cart_num	= I('post.cart_num');
		$ware_cart1	= I('post.ware_cart1');
		$cart_list1	= I('post.cart_list1');
		
		if( isset($_SESSION['uid']) && $_SESSION['uid'] !='' 
			&& isset($_SESSION['name']) && $_SESSION['name'] !='')
		{
			if( $cart_list1 == 'null'){
				$this->response( false , 2 , '没有规格' , true);
			}
			$Ware	= M('Ware');
			$Shopcar = M('Ware_shopcar');
			$Pay = M('Ware_pay');
			$res = $Shopcar->create();
			$data == array();//创建空数组
			$paylist = array();//存入支付数据库
			
			if( $res == false){
				$this->response( false , 3 , '没有数据' , true);
			}
			else{
				$list = $Ware->where("id={$id}")->find();
				$order_id = substr(date("ymdHi"),3,9).mt_rand(1,999);

				$data = array(
					'user_id'		=>	$_SESSION['uid'],
					'order_id'		=>	$order_id,
					'ware_id'		=>	$id,
					'cart_num'		=>	$cart_num,
					'ware_cart'		=>	$ware_cart1,
					'cart_list'		=>	$cart_list1,
					'head_img'		=>	$list['head_img'],
					'price'			=>	$list['price'],
					'ware_name'		=>	$list['ware_name'],
					'totalprice'	=>	$list['price']*$cart_num,
					
				);
				$paylist = array(
					'user_id'		=>	$_SESSION['uid'],
					'order_id'		=>	$order_id,
					'status'		=>	1,						//待交易
					'addtime'		=>	time(),
					'payprice'		=>	$list['price']*$cart_num
				);
				$res = $Shopcar->add($data);
				$ret = $Pay->add($paylist);
				
				$this->response( true , 0 , $data , true);
			}
			
		}
		$this->response( false , 1 , '未登录' ,true);
	}
	
	public function commentlist(){
		$id = I('post.id');
		
		$Comment = M('Ware_comment');
		$User = M('User');
		$Shopcar = M('Ware_shopcar');
		
		$list = $Comment->where("ware_id={$id}")->select();
		$data = array();
		if( $list == false){
			$this->response( false , 1 , '没有数据' , true);
		}
		else{
			foreach( $list as $vo){
				$user_id = $vo['user_id'];
				$email = $User->where("id={$user_id}")->getfield('email');
				$shopcar_id = $vo['shopcar_id'];
				$ware_cart = $Shopcar->where("id={$shopcar_id}")->getfield('ware_cart');
				$cart_list = $Shopcar->where("id={$shopcar_id}")->getfield('cart_list');
			}
			$data[] = array(
				'email'	=> $email,
				'addtime' => $vo['addtime'],
				'content' => $vo['content'],
				'ware_cart' => $ware_cart,
				'cart_list' => $cart_list
			);
			$this->response( true , 0 , $data , true);
		}
	}
	
	public function commentcount(){
		$id = I('post.id');

		$Comment = M('Ware_comment');
		
		$allcount = $Comment->where("ware_id={$id}")->count();
		$goodcount = $Comment->where("ware_id={$id} and good='好评'")->count();
		$midcount = $Comment->where("ware_id={$id} and good='中评'")->count();
		$badcount = $Comment->where("ware_id={$id} and good='差评' ")->count();

		$data = array(
			'allcount'	=> $allcount,
			'goodcount' => $goodcount,
			'midcount' => $midcount,
			'badcount' => $badcount,
			
		);
		$this->response( true , 0 , $data , true);
       
    }
}
